package com.starbaba.loanApiRecord.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.util.StringUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.loanApiRecord.model.LoanApiRecordModel;
import com.starbaba.loanApiRecord.service.LoanApiRecordService;
import com.starbaba.model.dictionary.Constant;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

public class LoanApiRecordServiceImpl implements LoanApiRecordService {
	
	private static BaseDao dao = new BaseDao(Constant.POOL_LIFE);

	@Override
	public List<LoanApiRecordModel> getApiRecordList(int iDisplayStart, int iDisplayLength, PageResponse pageResponse,
			Map<String, Object> paramsMap) {
		List<LoanApiRecordModel> dataList = new ArrayList<LoanApiRecordModel>();
		StringBuffer strSql = new StringBuffer();
		strSql.append("select a.source_id, b.name AS source_name,a.mobnum,a.petname,a.ctime, a.platform,a.first_union,c.name AS product_name,d.channel_name,a.result ");
		strSql.append("from loan_api_record a LEFT JOIN loan_product_info b on (a.source_id = b.id and a.type=b.union_type) ");
		strSql.append("left join comm_product_prdid c on (c.prdid = a.product_id) ");
		strSql.append("left join comm_product_channel d on (d.channel_code = a.channel) where 1=1 ");

		Integer sourceId = null;
		if(paramsMap.get("activityId") != null){
			sourceId = new Integer(paramsMap.get("activityId").toString());   //活动产品ID
		}	
		
		String sourceName = (String) paramsMap.get("activityName");    //活动产品名称
		String mobnum = (String) paramsMap.get("mobnum");    //手机号
		String startTime = (String) paramsMap.get("startTime");
		String endTime = (String) paramsMap.get("endTime");
		Integer result = null;
		if(paramsMap.get("status") != null){
			result = new Integer(paramsMap.get("status").toString());    //登录结果
		}
		String  platform = (String) paramsMap.get("platform");     //平台
		Integer productId = null;
		if(paramsMap.get("prdid") != null){
			productId = new Integer( paramsMap.get("prdid").toString());   //产品名称
		}
		
		String selectSql = "";
		if (sourceId != null) {
			selectSql += " and a.source_id = " + sourceId;
		}
		if (!StringUtils.isEmpty(sourceName)) {
			selectSql += " and b.name  = '" + sourceName + "'";
		}
		if (!StringUtils.isEmpty(mobnum)) {
			selectSql += " and a.mobnum = '" + mobnum + "' ";
		}
		if (!StringUtils.isEmpty(startTime)) {
			selectSql += " and a.ctime >= '" + startTime + " 00:00:00'";
		}
		if (!StringUtils.isEmpty(endTime)) {
			selectSql += " and a.ctime <= '" + endTime + " 23:59:59'";
		}
		if (result != null&&0==result.intValue()) {
			selectSql += " and a.first_union != 1 ";
		}
		if (result != null&&1==result.intValue()) {
			selectSql += " and a.first_union = 1 and  a.result  = " + result;
		}
		if (!StringUtils.isEmpty(platform)) {
			selectSql += " and a.platform = '" + platform + "'";
		}
		if (productId != null) {
			selectSql += " and a.product_id = " + productId;
		}
		
		strSql.append(selectSql);
		strSql.append(" order by a.ctime desc ");

		String countSql = "select count(a.source_id) from ("+strSql+") a";
		
		if (strSql.length() > 0 && iDisplayStart >= 0 && iDisplayLength > 0) {
			strSql.append(" limit " + iDisplayStart + " , " + iDisplayLength);
		}
		
		try {
			int count = dao.getInt(countSql);
			dataList = dao.list(strSql.toString(), LoanApiRecordModel.class);
			for (LoanApiRecordModel loanApiRecordModel : dataList) {
				if(1==loanApiRecordModel.getResult().intValue()&&1!=loanApiRecordModel.getFirst_union().intValue()){ //这里显示还是登陆失败
					loanApiRecordModel.setResult(0);
				}
				
				loanApiRecordModel.setMobnum(PhoneNumNideUtil.hideNum(loanApiRecordModel.getMobnum()));
				loanApiRecordModel.setPetname(PhoneNumNideUtil.hideName(loanApiRecordModel.getPetname()));
			}
			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		} catch (AppException e) {
			e.printStackTrace();
		}
		return dataList;
	}
	
	
	@Override
	public String exportLoanOrderCVS(List<LoanApiRecordModel> list) {
		
		StringBuffer sf = new StringBuffer();
		sf.append("活动ID,合作方,手机号码,姓名,登录时间,平台,产品,渠道,状态\r\n");

		for (LoanApiRecordModel record : list) {
			sf.append("'" + record.getSource_id()).append(",");
			sf.append(record.getSource_name()).append(",");
			sf.append(record.getMobnum()).append(",");
			sf.append(record.getPetname()).append(",");
			sf.append("'" + record.getCtime()).append(",");
			sf.append(record.getPlatform()).append(",");
			sf.append(record.getProduct_name()).append(",");
			sf.append(record.getChannel_name()).append(",");
			sf.append(record.getResult()).append(",");
			sf.append("\r\n");
		}
		return sf.toString();
	}
	
	
	public static void main(String args[]){
		LoanApiRecordServiceImpl impl = new LoanApiRecordServiceImpl();
		HashMap<String, Object> map =new HashMap<String, Object>();
		map.put("result",1);
		List<LoanApiRecordModel>  list  = impl.getApiRecordList(-1,-1,null,map);
		System.out.println(list);
	}
	
	
	
	
	
}
